package point

import (
	"fmt"
	"os/exec"
)

type WifiXApOpenPoint struct {
	index  int
	onOpen func()
}

func NewWifiXApOpenPoint(f func()) *WifiXApOpenPoint {
	return &WifiXApOpenPoint{
		onOpen: f,
	}
}

func (s *WifiXApOpenPoint) Get() any {
	return nil
}

func (m *WifiXApOpenPoint) Set(val ...any) bool {

	if wifixApSsid == "" {
		return false
	}

	if wifixApPassword == "" {
		return false
	}

	ifname := "wlan0"
	connectName := "wlan0"

	err := exec.Command("nmcli", "device", "wifi", "hotspot", "ifname", ifname, "con-name", connectName, "ssid", wifixApSsid, "password", wifixApPassword).Run()
	if err != nil {
		fmt.Println("open ap failed, err --> ", err)
		return false
	}

	wifixApState = "open"

	if m.onOpen != nil {
		m.onOpen()
	}

	return true
}

func (m *WifiXApOpenPoint) SetRoute(str string) PointInterface {
	m.index = int(str[len("wan")]) - '0'
	return m
}
